/*
 * ContractDetailPresenter.java
 * 
 * Copyright 2011 Matt Whipple <matt@mattwhipple.com>
 * 
 * This file is part of com.teamsuperfitness.apps
 * 
 * com.teamsuperfitness.apps is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * com.teamsuperfitness.apps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with com.teamsuperfitness.apps. If not, see http://www.gnu.org/licenses/.
 */
package com.teamsuperfitness.apps.client.presenters;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.teamsuperfitness.apps.client.service.ContractService;
import com.teamsuperfitness.apps.client.service.ContractServiceAsync;
import com.teamsuperfitness.apps.shared.events.ContractReqListUpdateEvent;
import com.teamsuperfitness.apps.shared.events.ContractSelectedEvent;
import com.teamsuperfitness.apps.shared.events.ContractSelectedEventHandler;
import com.teamsuperfitness.apps.shared.model.Contract;

/**
 * @author mwhipple
 * 
 */
public class ContractDetailPresenter implements IPresenter {
    private final SimpleEventBus eventBus;
    private final Display display;
    private final ContractServiceAsync service = GWT
        .create(ContractService.class);

    public interface Display {
        Widget asWidget();
        HasClickHandlers getAdder();
        HasClickHandlers getUpdater();
        Contract getContract();
        void clearData();
        void showContract(Contract contract);
    }

    public ContractDetailPresenter(SimpleEventBus eventBus, Display view) {
        this.eventBus = eventBus;
        this.display = view;
        bind();
    }

    public void bind() {
        eventBus.addHandler(
            ContractSelectedEvent.TYPE,
            new ContractSelectedEventHandler() {
                public void onContractSelected(ContractSelectedEvent event) {
                    display.showContract(event.getContract());
                }
            });

        display.getAdder().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                service.add(display.getContract(), new AsyncCallback<Void>() {
                    public void onFailure(Throwable caught) {
                    }

                    public void onSuccess(Void result) {
                        eventBus.fireEvent(new ContractReqListUpdateEvent());
                    }
                });
                display.clearData();
            }
        });
        display.getUpdater().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {

                service.update(
                    display.getContract(),
                    new AsyncCallback<Void>() {
                        public void onFailure(Throwable caught) {
                        }

                        public void onSuccess(Void result) {
                            eventBus
                                .fireEvent(new ContractReqListUpdateEvent());
                        }
                    });
                display.clearData();
            }
        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.teamsuperfitness.apps.client.presenters.IPresenter#go(com.google.
     * gwt.user.client.ui.Panel)
     */
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }

}
